package com.swotxu.oldwebproject.session.http;

import com.swotxu.oldwebproject.redis.RedisManager;
import com.swotxu.oldwebproject.redis.connection.IRedisConnection;
import com.swotxu.oldwebproject.session.utils.RedisSessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;

/**
 * @Date: 2021/3/24 0024 16:32
 * @Author: swotxu
 */
public class RedisSessionResponseWrapper extends HttpServletResponseWrapper {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisSessionResponseWrapper.class);
    private IRedisConnection connection = RedisManager.getIRedisConnection();

    public RedisSessionResponseWrapper(HttpServletResponse response, RedisSessionRequestWrapper request) {
        super(response);
        HttpSession session = request.getSession();
        String sessionId = session.getId();

        Cookie cookie = new Cookie(RedisSessionUtil.SESSION_TOKEN, sessionId);
        cookie.setPath(request.getContextPath());
        response.addCookie(cookie);

        String setCookie = new StringBuilder(RedisSessionUtil.SESSION_TOKEN).append("=").append(sessionId)
                .append(";Path=").append(request.getContextPath()).append(";HttpOnly").toString();
        response.setHeader("Set-Cookie", setCookie);

        response.setHeader(RedisSessionUtil.SESSION_TOKEN, sessionId);
    }
}
